﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaymuseMS.Models;

namespace MaymuseMS.DAL {
    public class UnitOfWork : IDisposable {
        private MaymuseMSContext context = new MaymuseMSContext();
        private SupplierRepository _supplierRepository;
        private PartsDepositoryRepository _partsDepositoryRepository;
        private SemifinishedDepositoryRepository _semifinishedDepositoryRepository;
        private FinishedDepositoryRepository _finishedDepositoryRepository;
        private ProductItemRepository _productItemRepository;
        private PartsRepository _partsRepository;
        private SemifinishedRepository _semifinishedRepository;
        private FinishedRepository _finishedRepository;
        private ProductRepository<Product> _productRepository;
        private ConfigurationRepository _configurationRepository;
        private ConfigurationItemRepository _configurationItemRepository;
        private SupplierUserRepository _supplierUserRepository;
        private OrderRepository _orderRepository;
        private OrderItemRepository _orderItemRepository;
        private PartsInDepositoryRepository _partsInDepositoryRepository;
        private SemifinishedInDepositoryRepository _semifinishedInDepositoryRepository;
        private FinishedInDepositoryRepository _finishedInDepositoryRepository;
        private PartsOutDepositoryRepository _partsOutDepositoryRepository;
        private SemifinishedOutDepositoryRepository _semifinishedOutDepositoryRepository;
        private FinishedOutDepositoryRepository _finishedOutDepositoryRepository;
        private InDepositoryRepository _inDepositoryRepository;
        private DepositoryItemRepository _depositoryItemRepository;
        private ProduceOrderRepository _produceOrderRepository;
        private OutDepositoryRepository _outDepositoryRepository;
        private DepositoryRepository _depositoryRepository;

        public DepositoryRepository DepositoryRepository {
            get {
                if (_depositoryRepository == null) {
                    _depositoryRepository = new DepositoryRepository(context);
                }
                return _depositoryRepository;
            }
        }
        public OutDepositoryRepository OutDepositoryRepository {
            get {
                if (_outDepositoryRepository == null) {
                    _outDepositoryRepository = new OutDepositoryRepository(context);
                }
                return _outDepositoryRepository;
            }
        }
        public ProduceOrderRepository ProduceOrderRepository {
            get {
                if (_produceOrderRepository == null) {
                    _produceOrderRepository = new ProduceOrderRepository(context);
                }
                return _produceOrderRepository;
            }
        }
        public DepositoryItemRepository DepositoryItemRepository {
            get {
                if (_depositoryItemRepository == null) {
                    _depositoryItemRepository = new DepositoryItemRepository(context);
                }
                return _depositoryItemRepository;
            }
        }
        public InDepositoryRepository InDepositoryRepository {
            get {
                if (_inDepositoryRepository == null) {
                    _inDepositoryRepository = new InDepositoryRepository(context);
                }
                return _inDepositoryRepository;
            }
        }
        public FinishedOutDepositoryRepository FinishedOutDepositoryRepository {
            get {
                if (_finishedOutDepositoryRepository == null) {
                    _finishedOutDepositoryRepository = new FinishedOutDepositoryRepository(context);
                }
                return _finishedOutDepositoryRepository;
            }
        }
        public SemifinishedOutDepositoryRepository SemifinishedOutDepositoryRepository {
            get {
                if (_semifinishedOutDepositoryRepository == null) {
                    _semifinishedOutDepositoryRepository = new SemifinishedOutDepositoryRepository(context);
                }
                return _semifinishedOutDepositoryRepository;
            }
        }
        public PartsOutDepositoryRepository PartsOutDepositoryRepository {
            get {
                if (_partsOutDepositoryRepository == null) {
                    _partsOutDepositoryRepository = new PartsOutDepositoryRepository(context);
                }
                return _partsOutDepositoryRepository;
            }
        }
        public FinishedInDepositoryRepository FinishedInDepositoryRepository {
            get {
                if (_finishedInDepositoryRepository == null) {
                    _finishedInDepositoryRepository = new FinishedInDepositoryRepository(context);
                }
                return _finishedInDepositoryRepository;
            }
        }
        public SemifinishedInDepositoryRepository SemifinishedInDepositoryRepository {
            get {
                if (_semifinishedInDepositoryRepository == null) {
                    _semifinishedInDepositoryRepository = new SemifinishedInDepositoryRepository(context);
                }
                return _semifinishedInDepositoryRepository;
            }
        }
        public PartsInDepositoryRepository PartsInDepositoryRepository {
            get {
                if (_partsInDepositoryRepository == null) {
                    _partsInDepositoryRepository = new PartsInDepositoryRepository(context);
                }
                return _partsInDepositoryRepository;
            }
        }
        public OrderItemRepository OrderItemRepository {
            get {
                if (_orderItemRepository == null) {
                    _orderItemRepository = new OrderItemRepository(context);
                }
                return _orderItemRepository;
            }
        }
        public OrderRepository OrderRepository {
            get {
                if (_orderRepository == null) {
                    _orderRepository = new OrderRepository(context);
                }
                return _orderRepository;
            }
        }
        public SupplierUserRepository SupplierUserRepository {
            get {
                if (_supplierUserRepository == null) {
                    _supplierUserRepository = new SupplierUserRepository(context);
                }
                return _supplierUserRepository;
            }
        }
        public ConfigurationItemRepository ConfigurationItemRepository {
            get {
                if (_configurationItemRepository == null) {
                    _configurationItemRepository = new ConfigurationItemRepository(context);
                }
                return _configurationItemRepository;
            }
        }
        public ConfigurationRepository ConfigurationRepository {
            get {
                if (_configurationRepository == null) {
                    _configurationRepository = new ConfigurationRepository(context);
                }
                return _configurationRepository;
            }
        }
        public ProductRepository<Product> ProductRepository {
            get {
                if (_productRepository == null) {
                    _productRepository = new ProductRepository<Product>(context);
                }
                return _productRepository;
            }
        }
        public FinishedRepository FinishedRepository {
            get {
                if (_finishedRepository == null) {
                    _finishedRepository = new FinishedRepository(context);
                }
                return _finishedRepository;
            }
        }

        public SemifinishedRepository SemifinishedRepository {
            get {
                if (_semifinishedRepository == null) {
                    _semifinishedRepository = new SemifinishedRepository(context);
                }
                return _semifinishedRepository;
            }
        }
        public PartsRepository PartsRepository {
            get {
                if (_partsRepository == null) {
                    _partsRepository = new PartsRepository(context);
                }
                return _partsRepository;
            }
        }
        public ProductItemRepository ProductItemRepository {
            get {
                if (_productItemRepository == null) {
                    _productItemRepository = new ProductItemRepository(context);
                }
                return _productItemRepository;
            }
        }
        public SupplierRepository SupplierRepository {
            get {
                if (this._supplierRepository == null)
                    this._supplierRepository = new SupplierRepository(context);
                return _supplierRepository;
            }
        }

        public PartsDepositoryRepository PartsDepositoryRepository {
            get {
                if (_partsDepositoryRepository == null) {
                    _partsDepositoryRepository = new PartsDepositoryRepository(context);
                }
                return _partsDepositoryRepository;
            }
        }

        public SemifinishedDepositoryRepository SemifinishedDepositoryRepository {
            get {
                if (_semifinishedDepositoryRepository == null) {
                    _semifinishedDepositoryRepository = new SemifinishedDepositoryRepository(context);
                }
                return _semifinishedDepositoryRepository;
            }
        }

        public FinishedDepositoryRepository FinishedDepositoryRepository {
            get {
                if (_finishedDepositoryRepository == null) {
                    _finishedDepositoryRepository = new FinishedDepositoryRepository(context);
                }
                return _finishedDepositoryRepository;
            }
        }

        public void Save() {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing) {
            if (!this.disposed) {
                if (disposing) {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}